<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
    <TITLE>Linux Terminal Simulator - HTML2</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#000000" TEXT="#00FF00">
    <H1>Linux Terminal Simulator - HTML2</H1>
    <HR>
    
    <PRE ID="output">Welcome to Linux Terminal Simulator (HTML2)
Type 'help' for available commands

</PRE>
    
    <FORM NAME="terminal">
        <B>user@linux:~$</B> 
        <INPUT TYPE="text" NAME="input" SIZE="60">
        <INPUT TYPE="button" VALUE="Execute" onClick="executeCommand()">
    </FORM>
    
    <SCRIPT LANGUAGE="JavaScript">
        <!--
        var currentPath = '/home/user';
        var fileSystem = {
            '/': {
                'home': {
                    'user': {
                        'test.txt': 'Hello World!\nThis is a test file.',
                        'documents': {},
                        'downloads': {}
                    }
                },
                'etc': {
                    'passwd': 'root:x:0:0:root:/root:/bin/bash\nuser:x:1000:1000:user:/home/user:/bin/bash'
                }
            }
        };
        
        function executeCommand() {
            var input = document.terminal.input.value;
            document.terminal.input.value = '';
            
            appendOutput('user@linux:' + (currentPath == '/home/user' ? '~' : currentPath) + '$ ' + input + '\n');
            
            var args = input.split(' ');
            var cmd = args[0];
            
            if (cmd == 'help') {
                showHelp();
            } else if (cmd == 'ls') {
                listFiles();
            } else if (cmd == 'pwd') {
                appendOutput(currentPath + '\n');
            } else if (cmd == 'cd') {
                changeDirectory(args[1]);
            } else if (cmd == 'cat') {
                catFile(args[1]);
            } else if (cmd == 'whoami') {
                appendOutput('user\n');
            } else if (cmd == 'date') {
                appendOutput(new Date().toString() + '\n');
            } else if (cmd == 'clear') {
                document.getElementById('output').innerHTML = '';
            } else if (input != '') {
                appendOutput('bash: ' + cmd + ': command not found\n');
            }
        }
        
        function showHelp() {
            var helpText = 'Available commands:\n' +
                '  ls              - List directory contents\n' +
                '  pwd             - Print working directory\n' +
                '  cd [dir]        - Change directory\n' +
                '  cat <file>      - Display file content\n' +
                '  whoami          - Show current user\n' +
                '  date            - Show current date/time\n' +
                '  clear           - Clear screen\n' +
                '  help            - Show this help\n\n';
            appendOutput(helpText);
        }
        
        function listFiles() {
            var dir = getCurrentDir();
            if (dir) {
                for (var name in dir) {
                    appendOutput(name + '  ');
                }
                appendOutput('\n');
            }
        }
        
        function changeDirectory(path) {
            if (!path || path == '~') {
                currentPath = '/home/user';
                return;
            }
            
            if (path == '..') {
                var parts = currentPath.split('/');
                if (parts.length > 2) {
                    parts.pop();
                    currentPath = parts.join('/');
                    if (currentPath == '') currentPath = '/';
                }
                return;
            }
            
            var newPath = currentPath == '/' ? '/' + path : currentPath + '/' + path;
            if (getDirectoryByPath(newPath)) {
                currentPath = newPath;
            } else {
                appendOutput('cd: ' + path + ': No such file or directory\n');
            }
        }
        
        function catFile(name) {
            if (!name) {
                appendOutput('cat: missing operand\n');
                return;
            }
            
            var dir = getCurrentDir();
            if (dir && dir[name] && typeof dir[name] == 'string') {
                appendOutput(dir[name] + '\n');
            } else {
                appendOutput('cat: ' + name + ': No such file\n');
            }
        }
        
        function getCurrentDir() {
            return getDirectoryByPath(currentPath);
        }
        
        function getDirectoryByPath(path) {
            var parts = path.split('/');
            var current = fileSystem['/'];
            
            for (var i = 1; i < parts.length; i++) {
                if (parts[i] && current[parts[i]]) {
                    current = current[parts[i]];
                } else if (parts[i]) {
                    return null;
                }
            }
            
            return current;
        }
        
        function appendOutput(text) {
            var output = document.getElementById('output');
            output.innerHTML += text;
        }
        
        document.terminal.input.onkeypress = function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                executeCommand();
                return false;
            }
        }
        //-->
    </SCRIPT>
</BODY>
</HTML>